import unittest


# ---------------- 被测函数 ----------------
def reverse_string(s: str) -> str:
    return s[::-1]


def capitalize_words(text: str) -> str:
    """
    按空格分隔并将每个单词首字母大写。
    - 若输入为空，或包含连续空格则抛出 ValueError。
    """
    if not text.strip() or "  " in text:
        raise ValueError("文本为空或包含多余空格！")

    return " ".join(word.capitalize() for word in text.strip().split())


# ---------------- 单元测试 ----------------
class TestStringFunctions(unittest.TestCase):

    # 1. 反转字符串
    def test_reverse_string(self):
        self.assertEqual(reverse_string("hello"), "olleh")
        self.assertEqual(reverse_string(""), "")
        self.assertEqual(reverse_string("a"), "a")

    # 2. 单词首字母大写（正常路径）
    def test_capitalize_words(self):
        self.assertEqual(capitalize_words("hello world"), "Hello World")
        self.assertEqual(capitalize_words("python is great"), "Python Is Great")

    # 3. 空字符串或多余空格应抛异常
    def test_capitalize_words_invalid_input(self):
        with self.assertRaises(ValueError):
            capitalize_words("")                         # 空字符串
        with self.assertRaises(ValueError):
            capitalize_words("  too  many  spaces  ")    # 连续空格


if __name__ == "__main__":
    unittest.main()
